﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Video;

public class VideoWrapper : MonoBehaviour
{
    public VideoPlayer m_videoPlayer;
    private int m_frameA = 0;
    private int m_frameB = int.MaxValue;

    private readonly YieldInstruction m_WaitForEndOfFrame = new WaitForEndOfFrame();
    /// <summary>
    /// 视频准备完成的回调方法
    /// </summary>
    private Action m_prepareCompleted;
    /// <summary>
    /// 视频播放完成的回调方法
    /// </summary>
    private Action m_loopPointReached;
    private void OnValidate()
    {
        if (m_videoPlayer == null)
            m_videoPlayer = transform.VideoPlayer();
    }

    public void Awake()
    {
        m_videoPlayer.prepareCompleted += source =>
        {
            PrepareCompleted();
        };
        m_videoPlayer.loopPointReached += source =>
        {
            LoopPointReached();
        };
    }

    public void Play(string videoName, int frameA = 0, int frameB = 0)
    {
        if (!string.IsNullOrEmpty(videoName))
        {
            if (Application.isEditor)
            {
                var url = "file:///" + Path.Combine(Application.dataPath, "../Movie/" + videoName);
                m_videoPlayer.url = url.Replace("\\", "/");
            }
            else
            {
                if (Application.platform == RuntimePlatform.Android)
                    m_videoPlayer.url = Path.Combine(Application.streamingAssetsPath, videoName);
                else
                    m_videoPlayer.url = "file:///" + Path.Combine(Application.streamingAssetsPath, videoName);
            }
        }

        //UnityEngine.Debug.LogWarning($"Play:{videoName}");
        SetAB(frameA, frameB);
        //m_videoPlayer.Play();
        m_videoPlayer.Prepare();
    }

    public void Stop()
    {
        m_prepareCompleted = null;
        m_loopPointReached = null;
        
        StopAllCoroutines();
        m_videoPlayer.Stop();
    }
    
    public void SetAB(int frameA, int frameB)
    {
        m_frameA = frameA;
        m_frameB = frameB;

        StopAllCoroutines();
        if (m_frameA > 0 && m_frameB > 0)
        {
            StartCoroutine(LoopAB());
        }
    }
    
    IEnumerator LoopAB()
    {
        yield return new WaitUntil(()=>m_videoPlayer.isPlaying);
        
        while (m_videoPlayer.isPlaying)
        {
            yield return m_WaitForEndOfFrame;
            
            if (m_videoPlayer.frame >= m_frameB)
            {
                m_videoPlayer.frame = m_frameA;
                //m_videoPlayer.StepForward();
                //m_videoPlayer.Play();
                //UnityEngine.Debug.LogWarning($"Play:{m_videoPlayer.frame}");
            }
        }
    }

    public void PrepareCompleted()
    {
        //UnityEngine.Debug.LogWarning("----------PrepareCompleted----------");
        m_prepareCompleted?.Invoke();
        m_videoPlayer.Play();
    }
    public void LoopPointReached()
    {
        //UnityEngine.Debug.LogWarning("----------LoopPointReached----------");
        m_loopPointReached?.Invoke();
    }

    /// <summary>
    /// 添加视频准备完成的回调方法
    /// </summary>
    /// <param name="callback"></param>
    public void AddPrepareCompleted(Action callback)
    {
        m_prepareCompleted = callback;
    }
    /// <summary>
    /// 添加视频播放完成的回调方法
    /// </summary>
    /// <param name="callback"></param>
    public void AddLoopPointReached(Action callback)
    {
        m_loopPointReached = callback;
    }

    public void RemovePrepareCompleted()
    {
        m_prepareCompleted = null;
    }
    public void RemoveLoopPointReached()
    {
        m_loopPointReached = null;
    }
}
